package com.example.springboot3.service;
import cn.hutool.core.util.ObjectUtil;
import com.example.springboot3.common.Constants;
import com.example.springboot3.common.enums.ResultCodeEnum;
import com.example.springboot3.common.enums.RoleEnum;
import com.example.springboot3.entity.Account;
import com.example.springboot3.entity.Admin;
import com.example.springboot3.exception.CustomException;
import com.example.springboot3.mapper.AdminMapper;
import com.example.springboot3.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;
    public void add(Admin admin) {
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (ObjectUtil.isNotNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(admin.getPassword())){
            admin.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(admin.getName())){
            admin.setName(admin.getUsername());
        }
        admin.setRole(RoleEnum.ADMIN.name());
        adminMapper.insert(admin);
    }

    /**
     * 分页查询
     */
    public Admin selectById(Integer id){
        return adminMapper.selectById(id);
    }
    public List<Admin> selectAll(Admin admin){
        return adminMapper.selectAll(admin);
    }
    public PageInfo<Admin> selectPage(Admin admin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = this.selectAll(admin);
//        返回的数据要用分页包一下？
        return new PageInfo<>(list);
    }




    public void updateById(Admin admin) {
        adminMapper.updateById(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
//        遍历ids,删除
        for (Integer id : ids) {
            adminMapper.deleteById(id);
        }
    }


    /**
     * 登录
     */
    public Admin login(Account account) {
        Admin dbAdmin = adminMapper.selectByUsername(account.getUsername());
//        判断数据库中是否存在 如果不存在抛出异常
        if (ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
//        判断输入的密码与数据库中存的密码不一样时抛出异常，显示密码错误
        if (!dbAdmin.getPassword().equals(account.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
//       调用生成token 传入的数据是 用户ID-角色 密钥作为password
       String token = TokenUtils.createToken(dbAdmin.getId() + "-" + dbAdmin.getRole(), dbAdmin.getPassword());
//       把token放进去
        dbAdmin.setToken(token);
//        再返回
        return dbAdmin;
    }

    /**
     * 修改密码
     * @param account
     */
    public void updatePassword(Account account) {
//        修改密码的逻辑是
//        1.根据用户名从数据库中查询管理员信息
        Admin dbadmin = adminMapper.selectByUsername(account.getUsername());
//        2.如果用户不存在 抛出异常
        if (ObjectUtil.isNull(dbadmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
//        3.判断输入的密码是否与数据库存的密码一致
        if (!dbadmin.getPassword().equals(account.getPassword())) {
//            如果不一致 则抛出错误
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
//        4.更新新密码
        dbadmin.setPassword(account.getPassword());
//        5.更新数据库 保存到数据库中
        adminMapper.updateById(dbadmin);
    }
}
